<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="商家名称" prop="businessName">
        <el-input
          v-model="queryParams.businessName"
          placeholder="请输入商家名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经营类型" prop="businessType">
        <el-select v-model="queryParams.businessType" placeholder="请选择经营类型" clearable>
          <el-option
            v-for="dict in dict.type.business_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商家地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入商家地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址经纬度" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入地址经纬度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="responsiblePerson">
        <el-input
          v-model="queryParams.responsiblePerson"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人联系方式" prop="responsiblePhone">
        <el-input
          v-model="queryParams.responsiblePhone"
          placeholder="请输入负责人联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['integral:business:add']"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['integral:business:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['integral:business:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['integral:business:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="businessList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商家id" align="center" prop="businessId" />
      <el-table-column label="商家名称" align="center" prop="businessName" />
      <el-table-column label="经营类型" align="center" prop="businessType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.business_type" :value="scope.row.businessType" />
        </template>
      </el-table-column>
      <el-table-column label="营业执照" align="center" prop="license" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.license" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="商家地址" align="center" prop="address" />
      <el-table-column label="地址经纬度" align="center" prop="location" />
      <el-table-column label="负责人" align="center" prop="responsiblePerson" />
      <el-table-column label="负责人联系方式" align="center" prop="responsiblePhone" />
      <!-- <el-table-column label="商家描述" align="center" prop="remark" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['integral:business:edit']"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['integral:business:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商家信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商家名称" prop="businessName">
          <el-input v-model="form.businessName" placeholder="请输入商家名称" />
        </el-form-item>
        <el-form-item label="经营类型" prop="businessType">
          <el-select v-model="form.businessType" placeholder="请选择经营类型">
            <el-option
              v-for="dict in dict.type.business_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="营业执照" prop="license">
          <image-upload v-model="form.license" />
        </el-form-item>
        <el-form-item label="商家地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入商家地址" />
        </el-form-item>
        <el-form-item label="地址经纬度" prop="location">
          <el-input v-model="form.location" placeholder="请输入地址经纬度" />
        </el-form-item>
        <el-form-item label="负责人" prop="responsiblePerson">
          <el-input v-model="form.responsiblePerson" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="负责人联系方式" prop="responsiblePhone">
          <el-input v-model="form.responsiblePhone" placeholder="请输入负责人联系方式" />
        </el-form-item>
        <el-form-item label="商家描述" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入商家描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBusiness,
  getBusiness,
  delBusiness,
  addBusiness,
  updateBusiness
} from '@/api/integral/business'

export default {
  name: 'Business',
  dicts: ['business_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商家信息表格数据
      businessList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessName: null,
        businessType: null,
        license: null,
        address: null,
        location: null,
        responsiblePerson: null,
        responsiblePhone: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询商家信息列表 */
    getList() {
      this.loading = true
      listBusiness(this.queryParams).then((response) => {
        this.businessList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        businessId: null,
        businessName: null,
        businessType: null,
        license: null,
        address: null,
        location: null,
        responsiblePerson: null,
        responsiblePhone: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.businessId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加商家信息'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const businessId = row.businessId || this.ids
      getBusiness(businessId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改商家信息'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.businessId != null) {
            updateBusiness(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addBusiness(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const businessIds = row.businessId || this.ids
      this.$modal
        .confirm('是否确认删除商家信息编号为"' + businessIds + '"的数据项？')
        .then(function () {
          return delBusiness(businessIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'integral/business/export',
        {
          ...this.queryParams
        },
        `business_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
